//引入组件库
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var dialog = sm("do_Dialog");
var userUrl = require("url/user");
var circleUrl = require("url/circle");
var imHelper = require("helper/im");
var http = require("helper/http");
var hashdata = mm("do_HashData");
// 声明UI变量
var listview = ui("listView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);// 绑定item的数据
var noData = [{"template":1,"labelContent":"你的好友都已经加入了这个圈子"}];

var httpFriendsUrl = userUrl.url.friend.myFriendOutOfCircle;// 我的好友(不在某个圈子成员里)
/** **************处理页面-头部-begin**************** */
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
headerLeft.on("touch", function() {
	app.closePage();
});

// 右上角-确定
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "确定";
headerRight_lableNext.visible = true;
headerRight.on("touch", function() {
	var selectFriendIds = [];
	for (var i = 0; i < listdata.getCount(); i++) {
		oneData = listdata.getOne(i);
		if (oneData.checked) {
			selectFriendIds.push(oneData.friendId);
		}
	}
	if (selectFriendIds.length == 0) {
		nf.toast("至少选择一位好友!");
		return;
	}
	creatCircleInviteBath(selectFriendIds);

});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "邀请新成员"
})
header.refreshData();

/** **************处理页面-头部-end**************** */
// andriod返回键
page.on("back", function() {
	app.closePage();
});
var vsCircleId = null;
page.on("loaded", function() {
	vsCircleId=page.getData().circleId;
	loadData();
});


// 更改选中状态
page.on("friend_check_change_event", function(friend) {
	var oneData = null;
	for (var i = 0; i < listdata.getCount(); i++) {
		oneData = listdata.getOne(i);
		if (oneData.friendId == friend.friendId) {
			oneData.checked = friend.checked;
			listdata.updateOne(i, oneData);// 全部设为“不选中”或"选中"
		}
	}
	//listview.refreshItems();
})

function loadData() {
	var params = {
		circleId: vsCircleId
	}
	http.post(httpFriendsUrl, params, function(data) {
		if (data.friendList) {
			var friendList = data.friendList;
			if(friendList.length>0){
				var friendList2 = [];
				listdata.removeAll();
				for (var i = 0; i < friendList.length; i++) {
					friendList[i].checked = false;// 设为“不选中”
					if(friendList[i].circleMember==false){
						friendList2.push(friendList[i]);
					}
				}
				if(friendList2.length>0){
					listdata.addData(friendList2);
					listview.refreshItems();
				}else{
					noData = [{"template":1,"labelContent":"你的好友都加入了这个圈子"}];
					listdata.addData(noData);
					listview.refreshItems();
				}
			}else{
				noData = [{"template":1,"labelContent":"没有找到好友"}];
				listdata.addData(noData);
				listview.refreshItems();
			}
		}
	});
}

// 下拉列表，刷新数据
listview.on("pull", function(data) {
	if (data.state == 2) {
		loadData();
		listview.rebound();// 复位【一定要做】
	}
});

// 上拉列表，翻页数据
listview.on("push", function(data) {
	if (data.state == 2) {
		loadData();
		listview.rebound();// 复位【一定要做】
	}
});

// 添加圈子邀请(一次多个好友)
function creatCircleInviteBath(friendIdsList) {
	var friendIdsString = friendIdsList.join(';');// 好友列表字符串
	var params = {
		"circleId" : vsCircleId+"",//强制转为字符串，否则后台从integer转string报错
		"beInviterIds" : friendIdsString,
		msg : "圈子邀请"
	};
	http.post(circleUrl.url.invite.createBath, params, function(data) {
		app.closePage({invite_sucess:true});
	});

}